Skip to content

fix: frozen string literal issue for ruby 3.4.0 #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chaadow
Copy link

@chaadow chaadow commented Nov 17, 2024

Fixes

Fixes #136

To prepare for ruby 3.4.0 and making strings frozen by default, I've stumbled upon these string mutations in my specs while trying to send an email

future of frozen string literals in ruby : https://gist.github.com/fxn/bf4eed2505c76f4fca03ab48c43adc72

similar to fog/fog-aws#709

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

If you have questions, please file a support ticket.

Sorry, something went wrong.

@chaadow chaadow changed the title Fix frozen string literal issue for ruby 3.4.0 fix: frozen string literal issue for ruby 3.4.0 Nov 17, 2024
url = [add_version(''), *@url_path].join('/')
url = build_query_params(url, query_params) if query_params
url = [add_version(+''), *@url_path].join('/')
url = build_query_params(+url, query_params) if query_params

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this url is frozen because it is not “string literal” 🤔

Suggested change
url = build_query_params(+url, query_params) if query_params
url = build_query_params(url, query_params) if query_params

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. The line before does a join('/')

So it creates a string literal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@childish-sambino, @rakatyal, @garethpaul, @tmimura39. Sorry to bother you but could look at this one?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @tmimura39 - url is a string, but it's not a string literal. As proof:

irb(main):002> a = ["foo", "bar"]
=> ["foo", "bar"]
irb(main):003> a.map! { it << " world" }
(irb):3:in 'block in <top (required)>': can't modify frozen String: "foo" (FrozenError)
	from (irb):3:in 'Array#map!'
	from (irb):3:in '<main>'
	from <internal:kernel>:168:in 'Kernel#loop'
	from /Users/chrishowlett/.local/share/mise/installs/ruby/3.4.1/lib/ruby/gems/3.4.0/gems/irb-1.15.1/exe/irb:9:in '<top (required)>'
	from /Users/chrishowlett/.local/share/mise/installs/ruby/3.4.1/bin/irb:25:in 'Kernel#load'
	from /Users/chrishowlett/.local/share/mise/installs/ruby/3.4.1/bin/irb:25:in '<main>'
irb(main):004> a.join("/") << "world"
=> "foo/barworld"

Another bump for getting this merged - we depend on this gem and it's blocking our ability to enable frozen literals in 3.4, and will (probably) Just Break from Ruby 4

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! Idk why I thought it was back then. There are so many recent methods that create frozen strings

For example "false.to_s" creates a frozen string

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed. Thanks

To prepare for ruby 3.4.0 and making strings frozen by default, we add `+` operator to make the string mutable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

warning: literal string will be frozen in the future in ruby 3.4.0-preview2
4 participants